<template>
  <div id="bigPage">
    <div id="tip">主页 > 用户列表</div>
    <p id="abscissa">用户列表显示</p>
    <form action="../" id="form">
      <ul>
        <li id="uid">
          用户ID：<input
            type="text"
            placeholder="请输入ID"
            name="uid"
            v-model="uid"
          />
        </li>
        <li id="uname">
          用户名：<input
            type="text"
            placeholder="请输入用户名称"
            name="uname"
            v-model="uname"
          />
        </li>
        <li id="utype">
          用户类型：
          <select name="utype" id="" v-model="utype">
            <option value="999">选择用户类型</option>
            <option value="1">管理员</option>
            <option value="0">普通用户</option>
          </select>
        </li>
        <li id="get" @click="get(uid, uname, utype)">查找</li>
      </ul>
      <table cellspacing="0" id="table">
        <tr>
          <td class="id">用户ID</td>
          <td class="name">用户名</td>
          <td class="type">用户类型</td>
        </tr>
        <tr v-for="data in pagedata" :key="data.id">
          <td class="id">{{ data.uid }}</td>
          <td class="name">
            {{ data.uname.slice(0, 5) }}{{ data.uname.length > 5 ? "..." : "" }}
          </td>
          <td class="type" :style="Moods(data.utype)">
            {{ data.utype == 0 ? "普通用户" : "管理员" }}
          </td>
        </tr>
      </table>
    </form>
    <p id="pageButton">
      <button class="page" @click="PageTop">上一页</button>
      {{ this.page }} / {{ Math.ceil(listdata.length / 5) }}
      <button class="page" @click="PageBottom">下一页</button>
    </p>
  </div>
</template>
  
  <script>
import VueCookies from "vue-cookies";
import axios from "axios";

export default {
  mounted() {
    // 生命钩子，挂载完毕时调用
    //【自动获取所有的用户信息,suid后端不能为空，所以传个0过去】
    // 999表示管理员与普通用户都获取
    this.get(0, "", 999);
  },
  computed: {
    //用来计算目前的页数的数据
    pagedata: {
      get() {
        // console.log(this.listdata);
        return this.listdata.slice(
          (this.page - 1) * 4 + (this.page - 1),
          this.page * 4 + this.page
        );
        // return "";
      },
    },
  },
  methods: {
    Moods(utype) {
      // 返回style对象，对类型颜色改变
      return utype == "1" ? { color: "red" } : { color: "blue" };
    },
    get(suid, uname, utype) {
      this.uid = JSON.parse(VueCookies.get("uid"));
      if (!this.uid) {
        alert("未登录，请先登录！");
        return false;
      } else {
        // 获取查询信息后进行恢复数据
        this.uid = "";
        this.uname = "";
        this.utype = 999;
        // 发送异步请求获取用户信息
        axios({
          method: "POST",
          url: "http://localhost:8081/lo/intercept/user/getuserasconditions",
          params: {
            uid: VueCookies.get("uid"),
            ucookie: VueCookies.get("ucookie"),
            suid: suid, // 搜索条件的id
            uname: uname.trim(),
            utype: utype,
          },
          headers: {
            headers: {
              "Content-Type": "application/x-www-form-urlencoded",
            },
          },
        }).then(
          (response) => {
            // console.log(response.data);
            // 直接将信息展示
            this.listdata = response.data;
            this.page = this.listdata.length == 0 ? 0 : 1;
          },
          (error) => {
            alert("请求失败,请检查服务器是否开启！");
          }
        );
      }
    },
    PageTop() {
      this.uid = JSON.parse(VueCookies.get("uid"));
      if (!this.uid) {
        alert("未登录，请先登录！");
        return false;
      } else {
        // console.log(1);
        if (this.page > 1) {
          this.page -= 1;
        } else {
          alert("最前一页！");
        }
      }
    },
    PageBottom() {
      this.uid = JSON.parse(VueCookies.get("uid"));
      if (!this.uid) {
        alert("未登录，请先登录！");
        return false;
      } else {
        if (this.page < Math.ceil(this.listdata.length / 5)) {
          this.page += 1;
        } else {
          alert("最后一页！");
        }
      }
    },
  },
  data() {
    return {
      uid: "",
      uname: "",
      utype: 999, // 999表示管理员与普通用户
      page: 0, // 表示当前页面
      listdata: [
        // { uid: "1", uname: "张三", utype: "0" },
        // { uid: "2", uname: "台灯2", utype: "1" },
        // { uid: "3", uname: "台灯3", utype: "0" },
        // { uid: "4", uname: "台灯4", utype: "1" },
      ],
    };
  },
};
</script>
  
<style scoped>
@font-face {
  font-family: "iconfont";
  src: url("../fonts/iconfont_four.ttf") format("truetype");
}
* {
  list-style-type: none;
  font-family: "iconfont" !important;
}
#bigPage {
  width: 75%;
  height: 900px;
  position: absolute;
  top: 70px;
  left: 21%;
  background-color: #eee;
}
#bigPage #tip {
  width: 100%;
  height: 30px;
  background-color: #fff;

  line-height: 30px;
}

#bigPage #tip::before {
  content: "\f012b";
  padding-left: 20px;
  padding-right: 10px;
}

#bigPage #abscissa {
  text-align: center;
  font-size: 30px;
  color: #72787c;
  margin-top: 10px;
}

#form {
  margin-left: 5%;
}

#form li {
  display: inline-block;
}

#form #uid {
  margin-left: 30px;
  margin-right: 20px;
}

#form #uname {
  margin-right: 20px;
}

#form #get {
  width: 45px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  margin-left: 100px;
  background-color: #5cb85c;
  color: #fff;
  border: 0px;
  cursor: pointer; /* 经过有手指*/
}

#form #del {
  width: 45px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  margin-left: 30px;
  background-color: #d9534f;
  color: #fff;
  border: 0px;
  cursor: pointer; /* 经过有手指*/
}

#form th {
  border: 0px;
}

#form td {
  border: 1px solid #000;
}

#table {
  width: 86%;
  text-align: center;
  margin-left: 3%;
  margin-top: 30px;
}
#table td {
  height: 40px;
}

#table .set {
  width: 22%;
}
#table .set span {
  display: inline-block;
  width: 50px;
  height: 30px;
  line-height: 30px;
  color: #fff;
  cursor: pointer; /* 经过有手指*/
}
#table .set #look {
  margin-right: 20px;
  background-color: #337ab7;
}

#table .set #updata {
  background-color: #5bc0de;
}

#pageButton {
  display: inline-block;
  margin-left: 39%;
  text-align: center;
  font-size: 20px;
}

#pageButton .page {
  width: 80px;
  height: 40px;
  border-radius: 10px;
  cursor: pointer; /* 经过有手指*/
}

#pageButton .page:hover {
  background-color: #5bc0de;
}
</style>